<?php


class GoodsTypeModel extends Model{

  public $table="goods_type";

  function addGoodsType($data){
   
   $this->validate=array(
      array('gtname','nonull','类型名不能为空',2,3)
    );

    if($this->create($data)){

      $gtid=$this->add();

      if($gtid){
        if($this->updateCache()){
          return $gtid;
        }else{
          return false;
        }
      }
     
    }else{
      return false;

    }
   

}



  public function updateCache(){

    $data = $this->all();

    $cacheData=array();

    foreach($data as $v){

      $cacheData[$v['gtid']]=$v;
    }

    return cache("goodsType",$cacheData);
  }
  


  public function del_d($gtid){
 //删除属性表
   $data= M('attr_class')->where(array('gtid'=>$gtid))->all();

  
    // //删除属性值表
    if(!is_null($data)){


    foreach($data as $v){

       $index=$v['acid'];
    
        M('attr_value')->where(array('acid'=>$index))->del();
     }
    }
    
  //删除属性表
 
   $key= M('attr_class')->where(array('gtid'=>$gtid))->del();
    
   //删除商品类型表
    $all=$this->where(array('gtid'=>$gtid))->del();

  if($key&&$all){
    return true;
  }else{
    return false;
  }

  }
}